// @ts-nocheck
// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
const vscode = require('vscode');

// This method is called when your extension is activated
// Your extension is activated the very first time the command is executed

/**
 * @param {vscode.ExtensionContext} context
 */
function activate(context) {

	let disposable = vscode.commands.registerCommand('ldoc.code2md', function () {
		const panel = vscode.window.createWebviewPanel(
				'markdown',
				vscode.window.activeTextEditor.document.fileName.split("/").pop(),
				vscode.ViewColumn.Two,
				{
						enableForms:true
				}
		);

		var text = vscode.window.activeTextEditor.document.getText()
		var markText = readDoc(text)
		var markdownIt = require("markdown-it");
                var hljs = require('highlight.js');
		var md = new markdownIt({
                        //@todo 没起作用，之后再研究，猜测可能是vs中有访问控制
                        highlight: function (str, lang) {
                                if (lang && hljs.getLanguage(lang)) {
                                  try {
                                    return hljs.highlight(lang, str).value;
                                  } catch (__) {}
                                }
                            
                                return ''; // 使用额外的默认转义
                        }
                });
		var html = md.render(markText)
		// var showdown = require("showdown")
		// var converter = new showdown.Converter();
		// var html = converter.makeHtml(markText)

                html = "<html><body>\n" + html + "\n</body></html>";
                console.log(html)
		panel.webview.html = html
});

	context.subscriptions.push(disposable);
}


function readDoc(code) {
    const STATE_NO = 0 //无状态
    const STATE_START = 1 //markdown状态
    const STATE_FUN = 2 //code状态

    let stat = STATE_NO;
    let markdownText = ""
    let funMap = {}
    let funArr = [];
    let funName = ""

        code.split(/\r?\n/).forEach((/** @type {string} */ line) => {
                switch (stat) {
                        case STATE_NO:
                                if (line.startsWith("@start")) {
                                        stat = STATE_START
                                        break
                                }
                                if(line.startsWith("//")) {
                                        let i = line.indexOf("@")
                                        if(i < 0) {
                                                break
                                        }

                                        funName = line.substring(i+1)
                                        if(funName.length < 1) {
                                                break
                                        }
                                        funMap[funName] = ""
                                        stat = STATE_FUN
                                        break
                                }
                                break;
                        case STATE_START:
                                if(line.startsWith("@end")) {
                                        stat = STATE_NO;
                                        break;
                                }
                                if(line.startsWith("@") && line.length > 1) {
                                        funArr.push(line)
                                }
                                markdownText = markdownText + line + "\n"
                                break;
                        case STATE_FUN:
                                if(line.startsWith("//")) {
                                        let i = line.indexOf("@end")
                                        if(i >= 0) {
                                                stat = STATE_NO
                                                break
                                        }

                                }
                                funMap[funName] = funMap[funName] + line + "\n";
                                break


                }



        });

        for (let i in funArr) {
                let f = funArr[i]
                if (!f.substring(1) in funMap) {
                        continue
                }
                markdownText = markdownText.replace(f,"```\n" + funMap[f.substring(1)] + "```\n")
        }
        return markdownText





}

// This method is called when your extension is deactivated
function deactivate() {}

module.exports = {
	activate,
	deactivate
}
